package kuzmenkov.oip.controller;

import kuzmenkov.oip.common.I18nTool;
import org.springframework.web.jsf.FacesContextUtils;

import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * User: Michael Kuzmenkov
 * Date: 04.04.2009
 * Time: 4:17:11
 */
public class HeaderController extends ErrorSupport {
    private I18nTool i18nTool;

    private final static String LANG_COOKIE_KEY = "language";

    public HeaderController() {
        try {
            i18nTool = (I18nTool) FacesContextUtils.
                    getWebApplicationContext(FacesContext.getCurrentInstance()).
                    getBean("i18nTool");
        } catch (Exception e) {
            addError(e);
        }

    }

    public String setEnglish() {
        try {
            i18nTool.setLocale(new Locale("en"));
            //storeLangCookie("en");
        } catch (Exception e) {
            addError(e);
            return "failure";
        }
        return "success";
    }

    public String setRussian() {
        try {
            i18nTool.setLocale(new Locale("ru"));
            //storeLangCookie("ru");
        } catch (Exception e) {
            addError(e);
            return "failure";
        }
        return "success";
    }

//    private void storeLangCookie(String lang) {
//        // create cookies
//        HttpServletResponse httpServletResponse =
//                (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
//        Cookie cookie = new Cookie(LANG_COOKIE_KEY, lang);
//        cookie.setMaxAge(365);
//        cookie.setComment("A language");
//        httpServletResponse.addCookie(cookie);
//    }
}